package com.yy.permissions_general.utils;

import com.yy.permissions_general.common.constant.SystemConstant;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.InputStream;

@Component
public class FTP {

    public static String ip;

    public static String username;

    public static String password;

    @Value("${custom.ftp.ip}")
    public void setIp(String ip) {
        FTP.ip = ip;
    }

    @Value("${custom.ftp.username}")
    public void setUsername(String username) {
        FTP.username = username;
    }

    @Value("${custom.ftp.password}")
    public void setPassword(String password) {
        FTP.password = password;
    }

    /**
     * 上传文件
     *
     * @param filename
     * @param input
     * @param dir
     * @return
     */
    public static boolean upload(String filename, InputStream input, String dir) throws IOException {
        boolean success = false;
        FTPClient ftp = new FTPClient();
        try {
            ftp.setControlEncoding(SystemConstant.UTF8);
            ftp.connect(ip);
            ftp.login(username,password);
            int reply = ftp.getReplyCode();
            if (!FTPReply.isPositiveCompletion(reply)) {
                ftp.disconnect();
                return false;
            }
            ftp.setFileType(2);
            ftp.enterLocalPassiveMode();
            boolean flag = ftp.changeWorkingDirectory(dir);
            if (!flag) {
                ftp.makeDirectory(dir);
                ftp.changeWorkingDirectory(dir);
            }
            ftp.storeFile(filename, input);
            input.close();
            success = true;
        } catch (IOException var18) {
            var18.printStackTrace();
        } finally {
            ftp.logout();
            if (ftp.isConnected()) {
                try {
                    ftp.disconnect();
                } catch (IOException var17) {
                    var17.printStackTrace();
                }
            }
        }
        return success;
    }


    /**
     * 删除文件
     *
     * @param dir
     * @param name
     */
    public static void deleteFile(String dir, String name) {
        FTPClient ftp = new FTPClient();
        try {
            ftp.setControlEncoding(SystemConstant.UTF8);
            ftp.connect(ip);
            ftp.login(username, password);
            ftp.setFileType(2);
            ftp.changeWorkingDirectory(dir);
            ftp.deleteFile(name);
        } catch (IOException var13) {
            var13.printStackTrace();
        } finally {
            if (ftp.isConnected()) {
                try {
                    ftp.disconnect();
                } catch (IOException var12) {
                    var12.printStackTrace();
                }
            }
        }
    }

    /**
     * 创建文件
     *
     * @param dir
     */
    public static void mkdir(String dir) {
        FTPClient ftp = new FTPClient();
        try {
            ftp.setControlEncoding(SystemConstant.UTF8);
            ftp.connect(ip);
            ftp.login(username, password);
            ftp.setFileType(2);
            boolean flag = ftp.changeWorkingDirectory(dir);
            if (!flag) {
                ftp.makeDirectory(dir);
                ftp.changeWorkingDirectory(dir);
            }
        } catch (IOException var13) {
            var13.printStackTrace();
        } finally {
            if (ftp.isConnected()) {
                try {
                    ftp.disconnect();
                } catch (IOException var12) {
                    var12.printStackTrace();
                }
            }
        }
    }

    /**
     * 重命名文件夹
     *
     * @param dir    原路径
     * @param newDir 新路径
     */
    public static void renamedir(String dir, String newDir) {
        FTPClient ftp = new FTPClient();
        try {
            ftp.setControlEncoding(SystemConstant.UTF8);
            ftp.connect(ip);
            ftp.login(username, password);
            ftp.setFileType(2);
            boolean flag = ftp.changeWorkingDirectory(dir);
            if (flag) {
               ftp.rename(dir,newDir);
            }
        } catch (IOException var13) {
            var13.printStackTrace();
        } finally {
            if (ftp.isConnected()) {
                try {
                    ftp.disconnect();
                } catch (IOException var12) {
                    var12.printStackTrace();
                }
            }
        }
    }

    /**
     * 下载文件
     *
     * @param dir
     * @param filename
     * @return
     */
    public static InputStream download(String dir, String filename) {
        FTPClient ftp = new FTPClient();
        try {
            ftp.setControlEncoding(SystemConstant.UTF8);
            ftp.connect(ip);
            ftp.login(username, password);
            ftp.setFileType(2);
            ftp.changeWorkingDirectory(dir);
            ftp.enterLocalPassiveMode();
            InputStream var6 = ftp.retrieveFileStream(filename);
            return var6;
        } catch (IOException var14) {
            var14.printStackTrace();
        } finally {
            if (ftp.isConnected()) {
                try {
                    ftp.disconnect();
                } catch (IOException var13) {
                    var13.printStackTrace();
                }
            }
        }
        return null;
    }

    /**
     * 文件大小
     *
     * @param dir
     * @param filename
     * @return
     */
    public static Long getDownloadFileSize(String dir, String filename) {
        FTPClient ftp = new FTPClient();
        try {
            ftp.setControlEncoding(SystemConstant.UTF8);
            ftp.connect(ip);
            ftp.login(username, password);
            ftp.setFileType(2);
            ftp.changeWorkingDirectory(dir);
            ftp.setBufferSize(1024000);
            ftp.enterLocalPassiveMode();
            Long var6 = ftp.listFiles(filename)[0].getSize();
            return var6;
        } catch (IOException var14) {
            var14.printStackTrace();
        } finally {
            if (ftp.isConnected()) {
                try {
                    ftp.disconnect();
                } catch (IOException var13) {
                    var13.printStackTrace();
                }
            }
        }
        return null;
    }
}
